﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Accident
    {
        public Accident(){ }
        public int Add(Model.Accident model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Addr", SqlDbType.NVarChar,150),
					new SqlParameter("@Condition", SqlDbType.NVarChar,500),
					new SqlParameter("@Make", SqlDbType.NVarChar,500),
					new SqlParameter("@SgPrice", SqlDbType.Decimal,9),
					new SqlParameter("@LossPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,1500),
					new SqlParameter("@LossTime", SqlDbType.DateTime),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Customer", SqlDbType.NVarChar,50),
					new SqlParameter("@Driver", SqlDbType.NVarChar,50)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.Addr;
            parameters[3].Value = model.Condition;
            parameters[4].Value = model.Make;
            parameters[5].Value = model.SgPrice;
            parameters[6].Value = model.LossPrice;
            parameters[7].Value = model.Marks;
            parameters[8].Value = model.LossTime;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.InputTime;
            parameters[11].Value = model.CarCode;
            parameters[12].Value = model.Customer;
            parameters[13].Value = model.Driver;
            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accident_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Accident model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Addr", SqlDbType.NVarChar,150),
					new SqlParameter("@Condition", SqlDbType.NVarChar,500),
					new SqlParameter("@Make", SqlDbType.NVarChar,500),
					new SqlParameter("@SgPrice", SqlDbType.Decimal,9),
					new SqlParameter("@LossPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,1500),
					new SqlParameter("@LossTime", SqlDbType.DateTime),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Customer", SqlDbType.NVarChar,50),
					new SqlParameter("@Driver", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.Addr;
            parameters[3].Value = model.Condition;
            parameters[4].Value = model.Make;
            parameters[5].Value = model.SgPrice;
            parameters[6].Value = model.LossPrice;
            parameters[7].Value = model.Marks;
            parameters[8].Value = model.LossTime;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.InputTime;
            parameters[11].Value = model.CarCode;
            parameters[12].Value = model.Customer;
            parameters[13].Value = model.Driver;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accident_Update", parameters);
            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Accident_Delete", parameters);
            if (intResult > 0)
                return true;
            else
                return false;
        }
        public Model.Accident GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Accident model = new Model.Accident();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Accident_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(ds.Tables[0].Rows[0]["Time"].ToString());
                }
                model.Addr = ds.Tables[0].Rows[0]["Addr"].ToString();
                model.Condition = ds.Tables[0].Rows[0]["Condition"].ToString();
                model.Make = ds.Tables[0].Rows[0]["Make"].ToString();
                if (ds.Tables[0].Rows[0]["SgPrice"].ToString() != "")
                {
                    model.SgPrice = decimal.Parse(ds.Tables[0].Rows[0]["SgPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LossPrice"].ToString() != "")
                {
                    model.LossPrice = decimal.Parse(ds.Tables[0].Rows[0]["LossPrice"].ToString());
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                if (ds.Tables[0].Rows[0]["LossTime"].ToString() != "")
                {
                    model.LossTime = DateTime.Parse(ds.Tables[0].Rows[0]["LossTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputUserId"].ToString() != "")
                {
                    model.InputUserId = int.Parse(ds.Tables[0].Rows[0]["InputUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputTime"].ToString() != "")
                {
                    model.InputTime = DateTime.Parse(ds.Tables[0].Rows[0]["InputTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TrueName"].ToString() != "")
                {
                    model.InputTrueName = ds.Tables[0].Rows[0]["TrueName"].ToString();
                }
                model.CarCode = ds.Tables[0].Rows[0]["CarCode"].ToString();
                model.Customer = ds.Tables[0].Rows[0]["Customer"].ToString();
                model.Driver = ds.Tables[0].Rows[0]["Driver"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        public DataSet GetList(Model.Accident.AccidentInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  Id,ReserveId,Time,Addr,Condition,Make,SgPrice,LossPrice,Marks,LossTime,InputUserId,InputTime");
            strSql.Append(" FROM Accident where 1=1");
            if (model.startime.ToString() != "" && model.startime != null)
            {
                strSql.Append(" and Time <'" + model.startime.ToString() + "'");
            }
            if (model.endtime.ToString() != "" && model.endtime != null)
            {
                strSql.Append(" and Time >'" + model.endtime.ToString() + "'");
            }
            strSql.Append(" order by Id desc");
            return SqlHelper.Query(strSql.ToString());
        }
    }
}
